﻿using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using WebDav.Client;
using WebDav.Client.Exceptions;

namespace WebDavWinFormsClientDemo
{
    public partial class WebDavWinFormsClient : Form
    {
        private string _domain = "";
        private string _password = "";
        private string _path = "";
        private string _protocol = "";
        private string _username = "";

        public WebDavWinFormsClient()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBoxProtocol.SelectedIndex = 0;
            comboBoxMethod.SelectedIndex = 0;
        }


        private void SendRequest()
        {
            

            var uri = _protocol + "://" + _domain + "/" + _path;
            Console.WriteLine("Requesting URI: " + uri);
            var request = (HttpWebRequest)WebRequest.Create(uri);

            ServicePointManager.Expect100Continue = false;

            request.ProtocolVersion = new Version(1,1);

            request.PreAuthenticate = true;
            request.ContentType = "application/xml; charset=\"utf-8\"";
            request.Method = comboBoxMethod.Text;

            var credentials = new NetworkCredential(_username, _password); 
            if (credentials.UserName != null)
            {
                request.Credentials = credentials;
                string auth = "Basic " +
                              Convert.ToBase64String(
                                  Encoding.Default.GetBytes(credentials.UserName + ":" + credentials.Password));
                request.Headers.Add("Authorization", auth);
            }

            if (request.Method == "PROPFIND")
                request.Headers["Depth"] = "1";

            
            var bytes = System.Text.Encoding.UTF8.GetBytes(requestBox.Text);
            

            if (request.Method == "PROPFIND" || request.Method == "REPORT")
            {
                request.ContentLength = bytes.Length;
                using (var rs = request.GetRequestStream())
                {
                    rs.Write(bytes, 0, bytes.Length);
                }
            }


            requestHeader.Text = request.Headers.ToString();

            responseHeadBox.Clear();
            responseBodyBox.Clear();
            try
            {
                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    responseHeadBox.AppendText(response.Headers.ToString());

                    using (var responseStream = new StreamReader(response.GetResponseStream()))
                    {
                        responseBodyBox.AppendText(responseStream.ReadToEnd());
                    }
                }
            }
            catch (WebException e)
            {
                Console.WriteLine(@"WebException.Status: " + e.Status + @"; Message: " + e.Message);
                MessageBox.Show(e.Message, e.Status.ToString());
            }
        }


        private void sendButton_Click(object sender, EventArgs e)
        {
            _protocol = comboBoxProtocol.Text;
            _domain = textBoxServer.Text;
            _path = Regex.Replace(Regex.Replace(textBoxPath.Text, "^\\/+", ""), "\\/+$", "");
            _username = textBoxUsername.Text;
            _password = textBoxPassword.Text;

            SendRequest();
        }
    }
}